package com.book.book.service;


import com.book.book.enums.BookStatusEnum;
import com.book.book.mapper.bookInfoMapper;
import com.book.book.model.BookInfo;
import com.book.book.model.PageRequest;
import com.book.book.model.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class BookService {
    private static Logger logger = LoggerFactory.getLogger(Logger.class);

    @Autowired
    bookInfoMapper bookInfoMapper;

    public List<BookInfo> getList() {
//        BookDao bookDao=new BookDao();

        List<BookInfo> bookInfos = bookInfoMapper.getList();
        for (BookInfo bookInfo : bookInfos) {
            bookInfo.setStatusCN(BookStatusEnum.getStatusByCode(bookInfo.getStatus()).getDesc());
        }
        return bookInfos;
    }

    public Integer addBook(BookInfo bookInfo) {
        return bookInfoMapper.addBook(bookInfo);
    }

    public ResponseResult<BookInfo> getListByPage(PageRequest pageRequest) {
        //获取总的图书数
        Integer count = bookInfoMapper.count();
        logger.info("count: " + count);
        List<BookInfo> bookInfos = bookInfoMapper.selectBooksByPage(pageRequest);
        logger.info("offset: " + pageRequest.getOffset());
        for (BookInfo bookInfo : bookInfos) {
            bookInfo.setStatusCN(BookStatusEnum.getStatusByCode(bookInfo.getStatus()).getDesc());
        }
        return new ResponseResult<>(count, bookInfos, pageRequest);
    }

    public BookInfo queryBookById(Integer bookId) {
        return bookInfoMapper.queryBookById(bookId);
    }


    public void updateBook(BookInfo bookInfo) {
        bookInfoMapper.updateBook(bookInfo);
    }

    public void batchDeleteBook(List<Integer> ids) {
        bookInfoMapper.batchDeleteBook(ids);
    }
}
